package net.system.sys.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.core.base.struts.BaseAction;
import net.core.common.HttpUtil;
import net.system.base.bean.TAd;
import net.system.common.HttpUtils;
import net.system.sys.service.IADService;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

@Scope("request")
@Namespace("/system/ad")
@InterceptorRefs(@InterceptorRef("basicStack"))
@ParentPackage("struts-default")
@Results({ @Result(name = "error", location = "/login.jsp") })
public class ADAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	@Autowired
	IADService service;
	private List<TAd> list;

	private String recId;

	private TAd bean;
	
	private Map returnMap;

	public List<TAd> getList() {
		return list;
	}

	public void setList(List<TAd> list) {
		this.list = list;
	}

	public String getRecId() {
		return recId;
	}

	public void setRecId(String recId) {
		this.recId = recId;
	}

	public TAd getBean() {
		return bean;
	}

	public void setBean(TAd bean) {
		this.bean = bean;
	}

	public Map getReturnMap() {
		return returnMap;
	}

	public void setReturnMap(Map returnMap) {
		this.returnMap = returnMap;
	}
	
	@Action(value = "list", results = { @Result(name = "success", location = "/WEB-INF/pages/system/ADList.jsp") })
	public String list() {
		// 管理界面包含查询及列表内容
		list = service.getList();
		return SUCCESS;
	}

	@Action(value = "add", results = { @Result(name = "success", location = "/WEB-INF/pages/system/ADAdd.jsp") })
	public String add() {
		// 添加页面
		return SUCCESS;
	}

	@Action(value = "edit", results = { @Result(name = "success", location = "/WEB-INF/pages/system/ADEdt.jsp") })
	public String updUser() {
		// 变更页面
		// 如果 recId为空。则取session中的userId
		if (null == recId || "".equals(recId)) {
			recId = HttpUtils.getCurrentLoginUserId(getRequest());
		}
		bean = service.getBean(recId);
		return SUCCESS;
	}

	/**
	 * @return
	 */
	@Action(value = "save", results = { @Result(name = "success", location = "/WEB-INF/pages/system/ADEdt.jsp") })
	public String save() {
		// 保存信息
		// 保存数据
		HashMap reqMap = new HashMap();
		reqMap = HttpUtil.buildParameterMap(getRequest());
		System.out.println(reqMap.toString());
		// 如果 recId为空。则增加操作
		if (null == recId || "".equals(recId)) {
			service.insert(reqMap);
		} else {
			service.update(reqMap);
		}
		bean = service.getBean(recId);
		return SUCCESS;
	}

	@Action(value = "show", results = { @Result(name = "success", location = "/WEB-INF/pages/system/ADEdt.jsp") })
	public String show() {
		// 显示详细页面
		bean = service.getBean(recId);
		return SUCCESS;
	}

}
